package cn.edu.heuet.bnve.activity.ScheduleActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.database.ScheduleDao;
import cn.edu.heuet.bnve.databinding.ActivityNewHomeworkBinding;
import cn.edu.heuet.bnve.utils.ColorUtil;
import cn.edu.heuet.bnve.utils.DialogUtil;
import cn.edu.heuet.bnve.utils.StringUtil;
import cn.edu.heuet.bnve.utils.ToastUtil;

public class ModifyHomeworkActivity extends AppCompatActivity {
    private ActivityNewHomeworkBinding binding;

    private AlertDialog setAlarmTimeDialog;

    TextView mscheduleDay;
    TextView mendTime;
    TextView mcolor;
    EditText mdecription;
    TextView mHomeworkTitle;


    private boolean isVibrate = false;

    private Schedule modifySchedule = new Schedule();
    private ScheduleDao scheduleDao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_with_view_pager);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_new_homework);

        initData();
        initView();
        initEvent();
    }


    public static void show(Context context) {
        context.startActivity(new Intent(context, NewHomeworkActivity.class));
    }

    public void initData(){
        Intent getIntent = getIntent();
        modifySchedule = (Schedule) getIntent.getSerializableExtra("schedule");
    }

    public void initView(){
        binding.txHead.setText(R.string.text_modify_homework);
        mscheduleDay = binding.alarmDate;
        mendTime = binding.alarmEndTime;
        mcolor = binding.alarmColor;
        mdecription = binding.alarmDescription;
        mHomeworkTitle = binding.alarmTitle;


        if(modifySchedule!=null){
            mHomeworkTitle.setText(modifySchedule.getTitle());
            mscheduleDay.setText(modifySchedule.getYear()+"/"+modifySchedule.getMonth()+"/"+modifySchedule.getDay());
            mendTime.setText(String.format("%02d", modifySchedule.getEndTimeHour())+":"+String.format("%02d", modifySchedule.getEndTimeMinute()));
            mcolor.setText(ColorUtil.getColorName(modifySchedule.getAlarmColor()));
            mdecription.setText(modifySchedule.getDescription());
            binding.alarmRemind.setText(StringUtil.getAlarmText(modifySchedule.getAlarmTimeType()));
            binding.alarmLocal.setText(modifySchedule.getLocal());
            if(modifySchedule.getIsVibrate()==1){
                binding.swVibrate.setChecked(true);
            }

        }
    }

    public void initEvent(){
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.txSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                complete();
            }
        });
        binding.buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        final DialogInterface.OnClickListener setAlarmTimeListener =
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                binding.alarmRemind.setText(R.string.text_alarm_time0);
                                modifySchedule.setAlarmTimeType(0);
                                break;
                            case 1:
                                binding.alarmRemind.setText(R.string.text_alarm_time1);
                                modifySchedule.setAlarmTimeType(1);
                                break;
                            case 2:
                                binding.alarmRemind.setText(R.string.text_alarm_time2);
                                modifySchedule.setAlarmTimeType(2);
                                break;
                            case 3:
                                binding.alarmRemind.setText(R.string.text_alarm_time3);
                                modifySchedule.setAlarmTimeType(3);
                                break;

                        }
                    }
                };
        binding.layoutAlarmRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setAlarmTimeDialog == null) {
                    setAlarmTimeDialog = new AlertDialog.Builder(ModifyHomeworkActivity.this)
                            .setTitle(R.string.set_alarm_time_items)
                            .setItems(R.array.set_alarm_time_items, setAlarmTimeListener)
                            .create();
                }
                setAlarmTimeDialog.show();
            }
        });
        binding.layoutAlarmLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { startActivityForResult(new Intent(ModifyHomeworkActivity.this, SetLocalActivity.class), 2); }
        });

        mscheduleDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectDayDialog(mscheduleDay,ModifyHomeworkActivity.this,modifySchedule);
            }
        });
        mendTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectTimeDialog(mendTime,1,ModifyHomeworkActivity.this,modifySchedule);
            }
        });
        binding.layoutAlarmColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectColorDialog(mcolor,ModifyHomeworkActivity.this,modifySchedule);
            }
        });
        binding.swVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Vibrator vibrator;
                vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                if (!isVibrate) {
                    modifySchedule.setIsVibrate(1);
                    vibrator.vibrate(500);
                    isVibrate = true;
                } else {
                    modifySchedule.setIsVibrate(0);
                    vibrator.cancel();
                    isVibrate = false;
                }
            }
        });
        binding.buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                complete();
            }
        });
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2) {
            if (resultCode == 2) {
                if (data != null) {
                    String local = data.getStringExtra("local");
                    binding.alarmLocal.setText(local);
                    modifySchedule.setLocal(local);
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


    private void complete(){
        if (mHomeworkTitle.getText().length()!=0){
            modifySchedule.setTitle(mHomeworkTitle.getText().toString());
        }
        else {
            DialogUtil.showSimpleTipDialog(ModifyHomeworkActivity.this,"请输入作业标题");
            return;
        }
        if(mscheduleDay.getText().equals(getResources().getString(R.string.select_day))){
            DialogUtil.showSimpleTipDialog(ModifyHomeworkActivity.this,"请选择活动日期");
            return;
        }
        else{
            modifySchedule.setIsAllday(0);
            if(mendTime.getText().equals(getResources().getString(R.string.select_end_time))){
                DialogUtil.showSimpleTipDialog(ModifyHomeworkActivity.this,"请选择结束时间");
                return;
            }
        }
        if(mdecription.getText().length()!=0){
            modifySchedule.setDescription(mdecription.getText().toString());
        }
        else {
            modifySchedule.setDescription("无");
        }

        Intent intent=new Intent();
        intent.putExtra("schedule", modifySchedule);
        setResult(0, intent);

        scheduleDao = new ScheduleDao(ModifyHomeworkActivity.this);
        scheduleDao.updateDataById(modifySchedule.getId(),modifySchedule);
        ToastUtil.showMsg(ModifyHomeworkActivity.this,"修改成功！");
        finish();

    }

}